Bayesian Robots!
Contents
Bayesian Robots!#
We will use Bayesian optimization to efficiently tune machine learning algorithms for robot movement.
Visualizing the data#
Let’s try to plot the position of the robot and where the walls are according to the sensors. We can compute the coordinates of the detected wall using the angle of each sensor and basic geometry:
\( x_{wall} = x_{robot} + dist * cos(angle_{robot} + angle_{sensor}) \)
\( y_{wall} = y_{robot} + dist * sin(angle_{robot} + angle_{sensor}) \)
Where \(x_{robot}\) is the x-coordinate of the robot, \(dist\) is the distance measured by the sensor, \(angle_{robot}\) is the current direction the robot is facing and \(angle_{sensor}\) is the relative angle of the specific sensor.
The dataset and the paper do not give any information on how fast the robot moves and how fast it turns, so we have to guess these. It does say that it measures 9 samples per second. After some trial and error we get plausible results if we set the speed to 0.1 meter per second and the turning rate to about 2 degrees per second. Below is the resulting animation in which the robot is presented as a triangle (green when moving and red when turning). The dots are the assumed locations of walls. Although the visualization is probably not very precise, we can see that the robot follows the nearest wall.
import math
import sys
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from matplotlib import colors
from matplotlib.animation import FuncAnimation
from IPython.display import HTML
matplotlib.rcParams['animation.embed_limit'] = 100
fig, ax = plt.subplots()
fig.set_tight_layout(True)
cx, cy = 0, 0 # robot position
angle = 180 # robot direction
ax.clear()
robot = ax.scatter(cx,cy, color='r', s=100, marker=(3, 0, angle+30))
def update(n):
global angle, cx, cy, robot
curr_x, cl = X[n], y[n]
if cl==0:
cx+=math.cos(math.radians(angle))*0.012
cy+=math.sin(math.radians(angle))*0.012
elif cl==1:
angle -= 0.02
elif cl==2:
angle -= 0.9
elif cl==3:
angle += 0.02
if n%30==0: #speed things up by only plotting every n'th step
wall_points =np.array([[cx+dist*math.cos(math.radians(angle-180+i*15)),cy+dist*math.sin(math.radians(angle-180+i*15))]
for i, dist in enumerate(curr_x) if dist<2 ])
ax.clear()
ax.set_xlim(-10,5)
ax.set_ylim(-2,10)
#robot.remove();
robot = ax.scatter(cx,cy, color=('g' if cl==0 else 'r'), s=100, marker=(3, 0, angle+30))
ax.scatter(wall_points[:,0],wall_points[:,1], color='k', s=1)
plt.close(fig)
anim = FuncAnimation(fig, update, frames=np.arange(0, 5000), interval=1)
HTML(anim.to_jshtml())